﻿//-----------------------------------------------------------------------------------------//
//-     Writen by Raymond Honderdors
//-     2013-05-14
//-
//-     Code can be used as is, no warrenty
//-     https://solutionmanagementki.codeplex.com/
//-----------------------------------------------------------------------------------------//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AnalysisServices;
namespace SMK_SSAS
{
    /// <summary>
    /// 
    /// </summary>
    public class AMO_Roles
    {
        /// <summary>
        /// 
        /// </summary>
        public Microsoft.AnalysisServices.Role amorole = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Server"></param>
        /// <param name="RoleID"></param>
        public void SetAMORole(AMO_Server Server, string RoleID)
        {
            if (Server != null && Server.Isconnected())
            {
                if (Server.amoserver().Roles.Contains(RoleID))
                {
                    amorole = Server.amoserver().Roles.Find(RoleID);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Boolean HasMembers()
        {
            if (amorole != null)
            {
                if (amorole.Members.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SortedList<string, AMO_Key> Members()
        {
            SortedList<string, AMO_Key> result = new SortedList<string, AMO_Key>();
            foreach (Microsoft.AnalysisServices.RoleMember db in amorole.Members)
            {
                AMO_Key amok = new AMO_Key();
                amok.ID = db.Sid;
                amok.Name = db.Name;
                result.Add(amorole.Name + "\\" + db.Sid, amok);
            }
            return result;
        }
    }
}
